package untils

import (
	"fmt"
	"order-srv/basic/inits"
)

// 扣减库存
func DelStockFromRedis(goodsId int) bool {
	sprintf := fmt.Sprintf("StockId:%d", goodsId)
	result, err := inits.Rdb.LPop(inits.Ctx, sprintf).Result()
	if err != nil {
		return false
	}
	return result == "1"
}

// 同步redis库存
func SyncStockToRedis(goodsId int, stock int) {
	sprintf := fmt.Sprintf("StockId:%d", goodsId)
	for i := 0; i < stock; i++ {
		inits.Rdb.LPush(inits.Ctx, sprintf, 1)
	}
}

// redis库存自增自减
func IncrStockToRedis(goodsId int) {
	sprintf := fmt.Sprintf("StockId:%d", goodsId)
	inits.Rdb.LPush(inits.Ctx, sprintf, 1)

}

// 获取库存
func GetStockToRedis(goodsId int) int {
	sprintf := fmt.Sprintf("StockId:%d", goodsId)
	stock, err := inits.Rdb.LLen(inits.Ctx, sprintf).Result()
	if err != nil {
		return 0
	}
	return int(stock)
}
